package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/3/29 10:16
 * @apiNote
 */

public class Test02908MinimumSum {
    public static void main(String[] args) {
        int[] nums = {1,2,2,2};
        System.out.println(minimumSumTest(nums));
    }

    public static  int minimumSumTest(int[] nums) {
        int n = nums.length;
        int min = Integer.MAX_VALUE;
        int[] left = new int[n];
        left[0] = nums[0];
        for(int i = 1; i < n ;i++){
            left[i] = Math.min(left[i-1],nums[i]);
        }
        int[] right = new int[n];
        right[n-1] = nums[n-1];
        for(int i = n-2; i >= 0 ;i--){
            right[i] = Math.min(left[i+1],nums[i]);
        }
        for(int i = 1;i<n-1;i++){
            if(nums[i] > left[i-1] && nums[i] > right[i+1]){
                min = Math.min(min,left[i-1] + nums[i] + right[i+1]);
            }
        }
        return min == Integer.MAX_VALUE ? -1:min;
    }

    public static int minimumSum(int[] nums) {
        int n = nums.length;
        int min = Integer.MAX_VALUE;
        for (int j = 1; j < n; j++) {
            for(int i = 0; i < j; i++){
                if(nums[i] > nums[j]){
                    continue;
                }
                for(int k = j+1;k<n;k++){
                    if(nums[k] > nums[j]){
                        continue;
                    }
                    if(nums[i] < nums[j] && nums[k] < nums[j]){
                        min = Math.min(min, nums[i] + nums[j] + nums[k]);
                    }
                }
            }
        }
        return min == Integer.MAX_VALUE ? -1:min;
    }

    public static int minimumSum1(int[] nums) {
        int n = nums.length;
        int res = Integer.MAX_VALUE, min = Integer.MAX_VALUE;
        int[] left = new int[n];
        for (int i = 1; i < n; i++) {
            left[i] = min = Math.min(nums[i - 1], min);
        }
        int right = nums[n - 1];
        for (int i = n - 2; i > 0; i--) {
            if (left[i] < nums[i] && nums[i] > right) {
                res = Math.min(res, left[i] + nums[i] + right);
            }
            right = Math.min(right, nums[i]);
        }
        return min == Integer.MAX_VALUE ? -1:min;
    }

}
